/**
 * Created by zhaizy on 2015/3/27.
 */

angular.module("quoteInput.controller", [])

//<personInfo><certNumber>123454</certNumber><email>zzb520@baoxian.com</email><certificateType>99</certificateType><tel>18102260808</tel><name>123</name></personInfo>
    .controller("QuoteInputController", function ($scope, $state) {
        var steps = ["quote.platform", "quote.insureConfig", "quote.provider", "quote.input"];

        $scope.quoteInfo = {};
        //被保人
        $scope.quoteInfo.insuredInfo = {
            "personInfo": {
                "certNumber": "123454",
                "email": "zzb520@baoxian.com",
                "certificateType": "99",
                "tel": "18102260808",
                "name": "123"
            }
        }
        //投保人
        $scope.quoteInfo.applicant = {
            "personInfo": {
                "certNumber": "123454",
                "email": "zzb520@baoxian.com",
                "certificateType": "99",
                "tel": "18102260808",
                "name": "123"
            }
        }

        //联系人
        $scope.quoteInfo.contactsInfo = {
            "personInfo": {
                "certNumber": "123454",
                "email": "zzb520@baoxian.com",
                "certificateType": "99",
                "tel": "18102260808",
                "name": "123"
            }
        }
        //车主
        $scope.quoteInfo.ownerInfo = {
            "personInfo": {
                "certNumber": "123454",
                "email": "zzb520@baoxian.com",
                "certificateType": "99",
                "tel": "18102260808",
                "name": "123"
            }
        }

        $scope.quoteInfo.recentInsure = {
            "lastComId": "0",
            "drivingRegion": "2",
            "avkt": "小于3万公里"

        }

        //指定驾驶人
        $scope.quoteInfo.drivers = {
            "row": [
                {
                    "birthday": "2006-04-08",
                    "gender": "1",
                    "licensedDate": "2012-04-08",
                    "licenseNo": "788999",
                    "driverTypeCode": "A1",
                    "name": "123"
                },
                {
                    "birthday": "2006-04-08",
                    "gender": "1",
                    "licensedDate": "2012-04-08",
                    "licenseNo": "788999",
                    "driverTypeCode": "A1",
                    "name": "123"
                }
            ]
        }

        //获取状态的下标
        steps.getIndex = function (step) {
            for (var i = 0; i < steps.length; i++) {
                if (step == steps[i]) {
                    return i;
                }
            }
            return -1;
        }
        $scope.quoteData = {}
        $scope.quoteData.carOwner = "zhaizy";

        $scope.preClick = function () {
            console.log("preClick:" + $state.$current);
            var index = steps.getIndex($state.$current);
            console.log("current index is " + index);
            if (index - 1 >= 0) {
                $state.go(steps[index - 1]);
            }
        }

        $scope.nextClick = function () {
            console.log("next click:" + $state.$current);
            var index = steps.getIndex($state.$current);
            console.log("current index is " + index);
            if (index + 1 < steps.length) {
                $state.go(steps[index + 1]);
            } else {
                console.log("已经最后一个了:" + JSON.stringify($scope.quoteInfo));
            }
        }
    })

    .controller("InsureConfigController", function ($scope, $http, utils) {

        $scope.quoteInfo.insureConfig = {};
        $http({
            url: "/assets/config.txt",
            method: "GET"
        }).success(function (result) {
            console.log("@@##config.txt:" + JSON.stringify(result));
            $scope.quoteInfo.insureConfig = result;
            //var xml = utils.json2xml(result);
            //console.log("xml is "+xml);
        }).error(function (error) {
            alert(error)
        });

    })

    .controller("quoteProviderController", function ($scope, zzbHttp, zzbLocalCache, PreferenceKey) {
        //
        //CmdVer=100;CmdModule=PROVIDER;ToKen=test;CmdId=Search;CmdType=FHBX
        //jobId=820000861;comCode=1244191435;channel=2;zone=440100

        $scope.data = {};

        var ids = new Array();

        zzbHttp.post({
            CmdType: "FHBX",
            CmdModule: "PROVIDER",
            CmdId: "Search"
        }, {
            jobId: zzbLocalCache.get(PreferenceKey.PRE_KEY_EMPLOYEENUMBER),
            comCode: zzbLocalCache.get(PreferenceKey.PRE_KEY_BUSINESSOFFICE),
            channel: "2",
            zone: zzbLocalCache.get(PreferenceKey.PRE_ZONE_ID)
        }, "正在查询供应商...").then(function (result) {

            if (result.Code == "0" && result.Body && result.Body.Provider && result.Body.Provider.result) {
                $scope.data.providers = result.Body.Provider.result;

                for (var i in $scope.data.providers) {
                    console.log("@@##item is " + JSON.stringify($scope.data.providers[i].id));
                    //$scope.data.providers[i].
                    var Provider = $scope.data.providers[i];
                    var providers = $scope.data.providers[i].providers;
                    if (providers.length == 1) {
                        Provider.selectItem = providers[0];
                        Provider.selectId = providers[0].id;
                    } else {
                        Provider.selectItem = undefined;
                        Provider.selectId = undefined;
                    }

                }
                //console.log("@@##providers:" + $scope.data.providers);
            }

        }, function (error) {
            alert(error);
        });

        $scope.onItemClick = function (item) {
            console.log("on item click " + item.selectId);

            if (!item.selectItem) {
                console.log("@@##item click !");
                alert("请先选择分公司!");
                return;
            }

            if (item.isCheck) {
                item.isCheck = false;
                if (item.selectId) {
                    ids.pop(item.selectId);
                } else {
                    ids.pop(item.selectItem.id);
                }
            } else {
                item.isCheck = true;
                if (item.selectId) {
                    ids.push(item.selectId);
                } else {
                    ids.push(item.selectItem.id);
                }
            }
            console.log("@@## ids is " + JSON.stringify(ids));
        }

        $scope.onCheckChange = function (isCheck, id) {
            //alert("check :" + id + " " + isCheck);
            console.log("@@## providers :" + JSON.stringify($scope.data.providers));
        }

        $scope.selectChange = function (item) {

            console.log("selectChanges is " + JSON.stringify(item.selectItem.id));
        }

    })